# 515.在每个树行中找最大值
# 给定一棵二叉树的根节点root ，请找出该二叉树中每一层的最大值。
#
#
# 示例1：
# 输入: root = [1, 3, 2, 5, 3, null, 9]
# 输出: [1, 3, 9]
#
# 示例2：
# 输入: root = [1, 2, 3]
# 输出: [1, 3]

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
class Solution:
    # 还是层序便利的变体
    def largestValues(self, root):
        from collections import deque
        que = deque()
        res = []
        if not root:
            return res
        que.append(root)
        while que:
            tmp = []
            size = len(que)
            for i in range(size):
                node = que.popleft()
                tmp.append(node.val)
                if node.left:
                    que.append(node.left)
                if node.right:
                    que.append(node.right)
            res.append(max(tmp))
        return res

if __name__ == '__main__':
    a32 = TreeNode(7)
    a31 = TreeNode(15)
    a21 = TreeNode(9)
    a22 = TreeNode(20,a31,a32)
    a11 = TreeNode(3,a21,a22)
    tmp = Solution()
    res = tmp.largestValues(a11)
    print(res)
